<template>
    <view>
        <view class="title">
            {{title+'识别'}}
        </view>
        <u-divider text="点击上传" textSize="40rpx"></u-divider>
        <view class="upload">
            <u-upload
            	:fileList="fileList"
                @afterRead="afterRead"
                @delete="doDelete"
            	multiple
            	:maxCount="1"
            	:previewFullImage="true"
                width="40vw"
                height="25vh"
                previewImage
            ></u-upload>
        </view>
        <u-button 
            text="上传" 
            type="primary" 
            color="#ff6a00" 
            plain 
            :customStyle="{width:60+'vw',marginTop:30+'rpx'}"
            @click="upload"
        >
            
        </u-button>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                title:'',
                fileList:[]
            };
        },
        onLoad(data) {
            this.title = data.title
        },
        methods:{
            doDelete(event){
                this.fileList.splice(event.index, 1)
            },
            async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.fileList[fileListLen]
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						// url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							// setTimeout(() => {
							// 	resolve('success')
							// }, 1000)
						},
                        complete: () => {
                            setTimeout(() => {
                            	resolve('success')
                            }, 1000)
                        }
					});
				})
			},
            upload(){
                
                uni.showLoading({ title:"上传中"});
                
                let obj = uni.getStorageSync("uploadInfo") || null;
                
                obj = obj ? JSON.parse(obj) : [];
                
                uni.setStorageSync("uploadInfo",JSON.stringify(obj.concat(this.fileList)));
                
                setTimeout(()=>{
                    uni.hideLoading();
                    uni.showModal({
                        content:"我的->鉴定结果中查看"
                    })
                },1500);
                // uni.setStorageSync("");
            },
        }
    }
</script>

<style lang="scss">
    .title{
        width: 100vw;
        height: 20vh;
        @include flexBox();
        font-size: 30px;
        color: #ff6a00;
    }
    .upload{
        width: 40vw;
        height: 25vh;
        @include flexBox();
        margin: 50rpx auto 0 auto;
    }
    .submitBtn{
        width: 50vw;
        margin-top: 20rpx;
        
    }
</style>
